<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript" src="jquery.min.js"></script>
	<script type="text/javascript">
  $(function() {
    console.log("用户4444444444444444");
    $.ajax({url:"http://localhost:8080/ssm-v2/login/4",success:function(result){
      console.log(result);
      var ws = new WebSocket("ws://localhost:8080/ssm-v2/myHandler")
       ws.onopen = function () {
        console.log("onpen");
       ws.send("{}");
       }
       ws.onclose = function () {
        console.log("onclose");
       }
      // 收到消息
      ws.onmessage = function(evnt) {
          $("#msg").append("<p>(<font color='red'>" + evnt.data + "</font>)</p>");
          console.log(" 444444444 websocket.onmessage 444444444==  " +evnt.data );
      };
      
    }});
  })
</script>
<%--
<script>
	var path = '<%=basePath%>';
	var userId = 'lys';
	if(userId==-1){
		window.location.href="login.jsp";
	}
	var jspCode = userId+"_AAA";
	var websocket;
	if ('WebSocket' in window) {
		websocket = new WebSocket("ws://" + path + "wsMy?jspCode=" + jspCode);
	} else if ('MozWebSocket' in window) {
		websocket = new MozWebSocket("ws://" + path + "wsMy?jspCode=" + jspCode);
	} else {
		websocket = new SockJS("http://" + path + "wsMy/sockjs?jspCode=" + jspCode);
	}
	websocket.onopen = function(event) {
		console.log("WebSocket:已连接");
		console.log(event);
	};
	websocket.onmessage = function(event) {
		var data = JSON.parse(event.data);
		console.log("WebSocket:收到一条消息-norm", data);
		alert("WebSocket:收到一条消息");
	};
	websocket.onerror = function(event) {
		console.log("WebSocket:发生错误 ");
		console.log(event);
	};
	websocket.onclose = function(event) {
		console.log("WebSocket:已关闭");
		console.log(event);
	}
</script>

@Controller  
public class GarlicPriceController {  
    @Resource  
    MyWebSocketHandler myWebSocketHandler;  
    @RequestMapping(value = "GarlicPriceController/testWebSocket", method ={RequestMethod.POST,RequestMethod.GET}, produces = "application/json; charset=utf-8")  
    @ResponseBody  
    public String testWebSocket() throws IOException{  
        myWebSocketHandler.sendMessageToJsp(new TextMessage(new GsonBuilder().create().toJson("\"number\":\""+"GarlicPriceController/testWebSocket"+"\"")), "AAA");  
        return "1";  
    }  
      
}  
 --%>
  </head>
  
  <body>
 			<!-- 消息展示框 -->
            <div id="msg" style="width: 100%; height: 70%; border: 1px solid yellow;overflow: auto;"></div>
  </body>
</html>
